from django.db import models


# Create your models here.
# 用户名, 密码, 是否管理员, 是否vip, 账号余额, 是否上机,
class User(models.Model):
    username = models.CharField('用户名', max_length=24)
    password = models.CharField('密码', max_length=24)  # md5 + md5 + 盐(username)
    """
    1  =(md5)> abc  # 数据库
    1  =>  abc  # 登录临时计算出的结果
    
    1,zs,uio  # 123456zs,     =>  abd  =>  iop    z123456s   123zs456
    2,ls,def
    3,ww,xyz
    
    ?  =>  abc
    md5 加密结果库
    abc,123456
    abd,123456zs
    def,123qwe
    xyz,888888
    
    uio, abc
    iop,abd
    """

    is_admin = models.BooleanField('是否管理员', default=0)
    is_vip = models.BooleanField('是否vip', default=0)
    balance = models.IntegerField('余额', default=0)

    is_online = models.BooleanField('是否上机', default=0)

    class Meta:
        db_table = 'tb_user'

    def __str__(self):
        return self.username


class MoneyRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    c_time = models.DateTimeField(auto_now_add=True, verbose_name='充值时间')
    money = models.IntegerField('充值金额')
    give_money = models.IntegerField('赠送金额', default=0)

    class Meta:
        db_table = 'tb_moneyrecord'

    def __str__(self):
        return "%s-%s-%s" % (self.user.username, self.money, self.c_time)


class Discount(models.Model):
    money = models.IntegerField('充值金额')
    give_money = models.IntegerField('赠送金额')

    class Meta:
        db_table = 'tb_discount'

    def __str__(self):
        return "%s-%s" % (self.money, self.give_money)


# 上机记录
class UseRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    pc = models.ForeignKey('pc.Pc', on_delete=models.PROTECT)
    start_time = models.DateTimeField('上机时间', auto_now_add=True)
    stop_time = models.DateTimeField('下机时间', null=True)
    fee = models.IntegerField('费用', default=0)

    class Meta:
        db_table = 'tb_userecord'

